using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.Grid;
using System;

namespace Common.Base
{
	public class GridViewColumnButtonMenu : GridViewMenu
	{
		public GridViewColumnButtonMenu(GridView view) : base(view)
		{
		}
		protected override void CreateItems()
		{
			base.Items.Clear();
			DXSubMenuItem dXSubMenuItem = new DXSubMenuItem("Danh Sách Các Cột");
			dXSubMenuItem.Image = GridMenuImages.Column.Images[5];
			base.Items.Add(dXSubMenuItem);
			base.Items.Add(base.CreateMenuItem("Tuỳ Chọn Cột", GridMenuImages.Column.Images[4], "Customization", true));
			base.Items.Add(base.CreateMenuItem("Co Giãn Cột", GridMenuImages.Column.Images[6], "BestFit", true));
			base.Items.Add(this.CreateMenuCheckItem("Chọn Hết", false, null, "SelectAll", true, true));
			base.Items.Add(this.CreateMenuItem("Bỏ Chọn", null, "UnSelectAll", true, false));
			foreach (GridColumn gridColumn in base.View.Columns)
			{
				if (gridColumn.OptionsColumn.ShowInCustomizationForm)
				{
					dXSubMenuItem.Items.Add(base.CreateMenuCheckItem(gridColumn.GetTextCaption(), gridColumn.VisibleIndex >= 0, null, gridColumn, true));
				}
			}
		}
		protected override void OnMenuItemClick(object sender, EventArgs e)
		{
			if (!this.RaiseClickEvent(sender, null))
			{
				DXMenuItem dXMenuItem = sender as DXMenuItem;
				if (dXMenuItem != null)
				{
					if (dXMenuItem.Tag != null)
					{
						if (dXMenuItem.Tag is GridColumn)
						{
							GridColumn gridColumn = dXMenuItem.Tag as GridColumn;
							gridColumn.VisibleIndex = ((gridColumn.VisibleIndex >= 0) ? -1 : base.View.VisibleColumns.Count);
						}
						else
						{
							if (dXMenuItem.Tag.ToString() == "Customization")
							{
								base.View.ColumnsCustomization();
							}
							else
							{
								if (dXMenuItem.Tag.ToString() == "BestFit")
								{
									base.View.BestFitColumns();
								}
								else
								{
									if (!(dXMenuItem.Tag.ToString() == "Filter"))
									{
										if (dXMenuItem.Tag.ToString() == "SelectAll")
										{
											base.View.SelectAll();
										}
										else
										{
											if (dXMenuItem.Tag.ToString() == "UnSelectAll")
											{
												base.View.ClearSelection();
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
